define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/bill/index' + location.search,
                    add_url: 'wanlshop/bill/add',
                    edit_url: 'wanlshop/bill/edit',
                    del_url: 'wanlshop/bill/del',
                    multi_url: '',
                    table: 'wanlshop_bill',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'paymenttime',
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),  //导出表格名称
                    ignoreColumn: [0, 'operate', 'shop.avatar','spot.img'], //不导出字段
                    ignoreRow: [],
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('订单编号')},
                        {field: 'paymenttime', title: __('付款时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'is_commission', title: __('状态'),searchList: {"0":__('待分账'),"1":__('已分账')},formatter: Table.api.formatter.status},
                        {field: 'shop.avatar', title: __('商家头像'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop.shopname', title: __('商家名称')},
                        {field: 'pay.price', title: __('支付金额'),operate:false},
                        {field: 'charge', title: __('微信手续费'),operate:false},
                        {field: 'pay.score_price', title: __('积分抵扣金额'),operate:false},

                        {field: 'income_price', title: __('商家所得'),operate:false},
                        {field: 'platform_rake', title: __('平台抽成'),operate:false},
                        {field: 'pick_up_rake', title: __('自提点分佣'),operate:false},
                        {field: 'spot.img', title: __('自提点/门店头像'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'spot.name', title: __('自提点/门店名称')},
                    ]
                ],
                responseHandler: function (res) {
                    // 获取并展示平台总抽成
                    $('#platform-settled').text('已结算金额: ' + res.platform_rake_all);
                    $('#shop-settled').text('已结算金额: ' + res.income_price_all);
                    $('#spot-settled').text('已结算金额: ' + res.pick_up_rake_all);
                    $('#platform-unsettled').text('未结算金额: ' + res.platform_unsettled);
                    $('#shop-unsettled').text('未结算金额: ' + res.shop_unsettled);
                    $('#spot-unsettled').text('未结算金额: ' + res.spot_unsettled);

                    return res; // 返回数据给表格渲染
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});